package cn.wolfcode.wolf2w.controller;


import cn.wolfcode.wolf2w.annotation.RequireLogin;
import cn.wolfcode.wolf2w.annotation.UserParam;
import cn.wolfcode.wolf2w.domain.*;
import cn.wolfcode.wolf2w.query.TravelQueryObject;
import cn.wolfcode.wolf2w.redis.service.IUserinfoRedisService;
import cn.wolfcode.wolf2w.service.*;
import cn.wolfcode.wolf2w.util.JsonResult;
import cn.wolfcode.wolf2w.util.UMEditorUploader;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/travels")
public class TravelController {


    @Autowired
    private ITravelService travelService;
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IDestinationService destinationService;
    @Autowired
    private IUserinfoRedisService userinfoRedisService;
    @Autowired
    private ITravelCommentService travelCommentService;


    @GetMapping("/query")
    public Object query(@ModelAttribute("qo") TravelQueryObject qo) {
        Page<Travel> query = travelService.query(qo);
        return JsonResult.success(query);
    }

    @GetMapping("/detail")
    public Object detail(String id) {
        Travel travel = travelService.get(id);
        return JsonResult.success(travel);
    }

    @GetMapping("/getUser")
    public Object getUser(@UserParam UserInfo userInfo) {
        return JsonResult.success(userInfo);
    }


    @GetMapping("/input")
    public Object input(String id) {
        List<Destination> list = destinationService.list();
        return JsonResult.success(list);
    }

    @PostMapping("/saveOrUpdate")
    public Object saveOrUpdate(Travel travel, /*HttpServletRequest request*/@UserParam UserInfo token1) {
//        String token = request.getHeader("token");
//        UserInfo token1 = userinfoRedisService.getToken(token);
        travelService.saveOrUpdate(travel, token1);
        System.out.println(token1);
        return JsonResult.success(travel);
    }

    @PostMapping("/commentAdd")
    @RequireLogin
    public Object commentAdd(TravelComment travelComment, @UserParam UserInfo userInfo) {
        BeanUtils.copyProperties(userInfo, travelComment);
        travelComment.setUserId(userInfo.getId());
        travelCommentService.save(travelComment);
        return JsonResult.success();
    }


    //上传图片
    @RequestMapping("/contentImage")
    public String uploadUEImage(MultipartFile upfile, HttpServletRequest request) throws Exception {
        UMEditorUploader up = new UMEditorUploader(request);
        String[] fileType = {".gif", ".png", ".jpg", ".jpeg", ".bmp"};
        up.setAllowFiles(fileType);
        up.setMaxSize(10000); //单位KB
        up.upload(upfile);

        String callback = request.getParameter("callback");
        String result = "{\"name\":\"" + up.getFileName() + "\", \"originalName\": \"" + up.getOriginalName() + "\", \"size\": " + up.getSize()
                + ", \"state\": \"" + up.getState() + "\", \"type\": \"" + up.getType() + "\", \"url\": \"" + up.getUrl() + "\"}";
        result = result.replaceAll("\\\\", "\\\\");
        if (callback == null) {
            return result;
        } else {
            return "<script>" + callback + "(" + result + ")</script>";
        }
    }

    @GetMapping("/comments")
    public Object comments(String travelId) {
       List<TravelComment>  travelComment = travelCommentService.findByTraveId(travelId);
        return JsonResult.success(travelComment);
    }

}
